package com.ctgu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Author 嘉宾
 * @Data 2022/3/23 14:15
 * @Version 1.0
 */
@Service
public class MailServiceImpl {

    private static final String SENDER = "3210572811@qq.com";
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送普通邮件
     *
     * @param to      收件人
     * @param subject 主题（标题）
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(SENDER);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
            mailSender.send(message);
        } catch (Exception e) {
            System.out.println("发送普通邮件时发生异常!" + e);
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param fileList 附件
     */
    public void sendFileMail(String to, String subject, String content, List<File> fileList) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(SENDER);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            if (fileList != null && fileList.size() > 0) {
                for (File file : fileList) {
                    FileSystemResource fileSystemResource = new FileSystemResource(file);
                    String fileName = fileSystemResource.getFilename();
                    helper.addAttachment(fileName, fileSystemResource);
                }
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            System.out.println("发送带附件的邮件时发生异常！" + e);
        }
    }

    /**
     * 发送HTML静态文件的邮件
     *
     * @param to       收件人
     * @param subject  主题
     * @param content  内容
     * @param srcIdMap 需要替换的静态文件(给html中标记id不赋值的话，可以发送，但静态文件不会显示)，可为null表示纯HTML文件
     */
    public void sendHtmlMail(String to, String subject, String content, Map<String, String> srcIdMap) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(SENDER);
            helper.setTo(to);
            helper.setSubject(subject);
            //true指的是html邮件
            helper.setText(content, true);
            if (srcIdMap != null) {
                for (Map.Entry<String, String> entry : srcIdMap.entrySet()) {
                    FileSystemResource file = new FileSystemResource(new File(entry.getValue()));
                    helper.addInline(entry.getKey(), file);
                }
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            System.out.println("发送HTML静态文件的邮件时发生异常！" + e);
        }
    }

}